package com.xingyun.kotlintest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class utils1 {
    // 更新数据在子线程，可能有问题
    public interface Listener{
        void onResponse(String response);
    }
    /*
    utils.get("http://192.168.1.32/test.json",new utils.Listener(){
        @Override
        public void onResponse(String response) {
            text1.setText(response);
        }
    });
     */
    public static void get(final String path, final Listener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection conn = null;
                try {
                    URL url = new URL(path);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    InputStream is = conn.getInputStream();
                    try {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = is.read(buffer))!=-1) {
                            baos.write(buffer,0,len);
                        }
                        is.close();
                        baos.close();
                        String res = new String(baos.toByteArray());
                        listener.onResponse(res);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    assert conn != null;
                    conn.disconnect();
                }
            }
        }).start();
    }
    /*
    utils.post("http://192.168.1.32:8800/test",null,new utils.Listener(){
        @Override
        public void onResponse(String response) {
            Log.d("test","========");
        }
    });
     */
    public static void post(final String path, final String params, final Listener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection conn = null;
                try {
                    URL url = new URL(path);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Content-Type","application/json");
                    if(params!=null){
                        conn.setDoOutput(true);
                        OutputStream os = conn.getOutputStream();
                        byte[] requestBody = null;
                        requestBody = params.getBytes(StandardCharsets.UTF_8);
                        os.write(requestBody);
                        os.flush();
                        os.close();
                    }
                    InputStream is = conn.getInputStream();
                    try {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = is.read(buffer))!=-1) {
                            baos.write(buffer,0,len);
                        }
                        is.close();
                        baos.close();
                        String res = new String(baos.toByteArray());
                        listener.onResponse(res);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    assert conn != null;
                    conn.disconnect();
                }
            }
        }).start();
    }
    public static String getTime(){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
    }
}
